#!/usr/bin/env python

# Copyright (c) 2007 Marcelo Lira dos Santos
#
# Author: Marcelo Lira dos Santos <setanta@gmail.com>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
# USA

import os
import pygame
import config

class Controls:

    (
    CONTROL_GOTO_START,
    CONTROL_STEP_BACKWARD,
    CONTROL_PLAY_PAUSE,
    CONTROL_STEP_FORWARD,
    CONTROL_GOTO_END,
    ) = range(5)


    def __init__(self, surface):
        self.surface = surface

        self.controls_names = ['goto-start', 'step-backward', 'play-pause',
                               'step-forward', 'goto-end']

        self.controls = [pygame.image.load(os.path.join(config.data_dir,\
                            'control-goto-start.png')).convert_alpha(),
                         pygame.image.load(os.path.join(config.data_dir,\
                            'control-step-backward.png')).convert_alpha(),
                         pygame.image.load(os.path.join(config.data_dir,\
                            'control-play.png')).convert_alpha(),
                         pygame.image.load(os.path.join(config.data_dir,\
                            'control-step-forward.png')).convert_alpha(),
                         pygame.image.load(os.path.join(config.data_dir,\
                            'control-goto-end.png')).convert_alpha()]

        self.swap_control = pygame.image.load(os.path.join(config.data_dir,\
                            'control-pause.png')).convert_alpha()

        self._play_control = self.controls[Controls.CONTROL_PLAY_PAUSE]
        self._pause_control = self.swap_control

        self.control_width = self.controls[Controls.CONTROL_PLAY_PAUSE].\
                get_width()
        self.spacing = 20
        self.width = (self.control_width + self.spacing) * \
                     len(self.controls) - self.spacing

        self.height = self.controls[Controls.CONTROL_PLAY_PAUSE].get_height()

        x = (surface.get_width() - self.width) / 2
        y = surface.get_height() - self.height - self.spacing
        self.pos = (x, y)

        self.callbacks = {}
        for name in self.controls_names:
            self.callbacks[name] = None


    def set_callback(self, signal, callback):
        if not self.callbacks.has_key(signal):
            return False

        self.callbacks[signal] = callback

        return True


    def reset(self):
        self.controls[Controls.CONTROL_PLAY_PAUSE] = self._play_control
        self.swap_control = self._pause_control


    def on_mouse_click(self, mouse_pos, button):
        if button != 1: # LEFT
            return

        pos = (mouse_pos[0] - self.pos[0], mouse_pos[1] - self.pos[1])
        control = self._get_clicked_control(pos)

        if control == 'play-pause':
            self.play_pause()

        if control and self.callbacks[control]:
            self.callbacks[control]()

    def play_pause(self):
        swap = self.controls[Controls.CONTROL_PLAY_PAUSE]
        self.controls[Controls.CONTROL_PLAY_PAUSE] = self.swap_control
        self.swap_control = swap

    def _get_clicked_control(self, pos):
        x, y = pos

        if y < 0 or y > self.height:
            return None

        for i in range(len(self.controls)):
            control_x = (self.control_width + self.spacing) * i
            if x >= control_x and x <= control_x + self.control_width:
                control = self.controls_names[i]
                return control
            
        return None


    def draw(self, mouse_pos=None):
        x, y = self.pos

        for control in self.controls:
            self.surface.blit(control, (x, y))
            x += self.control_width + self.spacing

